package com.ziytek.web.citizen.util;

import com.ziytek.web.citizen.constant.SettingConstant;
import com.ziytek.web.citizen.enums.StepNameEnum;
import com.ziytek.web.citizen.model.bean.AddSettings;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author fenghx
 * @version v1.0
 * @className ChargeTypeUtil
 * @description 功能描述
 * @date 2023/7/11 15:04
 **/
@Slf4j
public class ChargeTypeUtil {

//    public static String getDataType(String type) {
//        return switch (type) {
//            case ServerConstant.DATA_TYPE_DISCHARGE -> "DISCHARGE";
//            case ServerConstant.DATA_TYPE_CIRCLE -> "CIRCLE";
//            case ServerConstant.DATA_TYPE_CHARGE -> "CHARGE";
//            case ServerConstant.DATA_TYPE_04 -> "CONFIG";
//            case ServerConstant.DATA_TYPE_CHECK -> "CHECK";
//            case ServerConstant.DATA_TYPE_06 -> "CAPACITY";
//            case ServerConstant.DATA_TYPE_HEART -> "HEART";
//            case ServerConstant.DATA_TYPE_WORK_STEP -> "STEP";
//            case ServerConstant.DATA_TYPE_GET_CAP -> "GET_CAP";
//            default -> "UNKNOWN";
//        };
//    }

    public static String getDataTypeMsg(String type) {
        return switch (type) {
            case SettingConstant.CONSTANT_CURRENT_DISCHARGE, SettingConstant.CONSTANT_POWER_DISCHARGE, SettingConstant.CONSTANT_RESISTANCE_DISCHARGE ->
                    "放电工步";
            case SettingConstant.CONSTANT_CURRENT_CONSTANT_VOLTAGE, SettingConstant.CONSTANT_CURRENT,
                 SettingConstant.CONSTANT_POWER_CHARGE ,
                 SettingConstant.CONSTANT_VOLTAGE ->
                    "充电工步";
            case SettingConstant.SHELVE -> "搁置工步";
            default -> "未知工步";
        };
    }

    /**
     * 判断是否是搁置工步
     *
     * @param setType 工步类型
     * @return true or false
     */
    public static boolean isNotShelfType(String setType) {
        return !StringUtil.isEmpty(setType) && !SettingConstant.SHELVE.equalsIgnoreCase(setType);
    }

    /**
     * 判断是否是放电工步
     *
     * @param setType 工步类型
     * @return true or false
     */
    public static boolean isDischargeType(String setType) {
        return !StringUtil.isEmpty(setType) && (SettingConstant.CONSTANT_CURRENT_DISCHARGE.equalsIgnoreCase(setType)
                || SettingConstant.CONSTANT_POWER_DISCHARGE.equalsIgnoreCase(setType)
                || SettingConstant.CONSTANT_RESISTANCE_DISCHARGE.equalsIgnoreCase(setType));
    }

    public static String getTypeMsg(String type) {
        switch (type) {
            case String s -> {
                if (isDischargeType(s)) {
                    return SettingConstant.SET_TYPE_DISCHARGE;
                }
                if (isShelveType(s)) {
                    return SettingConstant.SET_TYPE_SL;
                } else {
                    return SettingConstant.SET_TYPE_CHARGE;
                }
            }
        }
    }

    /**
     * 判断是否是充电工步
     *
     * @param setType 工步类型
     * @return true or false
     */
    public static boolean isChargeType(String setType) {
        return !StringUtil.isEmpty(setType)
                && (SettingConstant.CONSTANT_CURRENT_CONSTANT_VOLTAGE.equalsIgnoreCase(setType)
                || SettingConstant.CONSTANT_POWER_CHARGE.equalsIgnoreCase(setType)
                || SettingConstant.CONSTANT_CURRENT.equalsIgnoreCase(setType)
                || SettingConstant.CONSTANT_VOLTAGE.equalsIgnoreCase(setType));
    }


    /**
     * 执行工步类型
     *
     * @param setType 工步类型
     * @return String
     */
    public static String getChargeOrDischargeType(String setType) {
        // 放电
        if (isDischargeType(setType)) {
            return SettingConstant.SET_TYPE_DISCHARGE;
        }

        // 充电
        if (isChargeType(setType)) {
            return SettingConstant.SET_TYPE_CHARGE;
        }

        // 搁置
        return isShelveType(setType) ? SettingConstant.SET_TYPE_SL : null;
    }

    /**
     * 判断是否是搁置工步
     *
     * @param setType 工步类型
     * @return true or false
     */
    public static boolean isShelveType(String setType) {
        return !StringUtil.isEmpty(setType) && SettingConstant.SHELVE.equalsIgnoreCase(setType);
    }


    public static List<String> getChargeTypeList() {
        List<String> list = new ArrayList<>();
        list.add(SettingConstant.CONSTANT_CURRENT_DISCHARGE);
        list.add(SettingConstant.CONSTANT_POWER_DISCHARGE);
        list.add(SettingConstant.CONSTANT_RESISTANCE_DISCHARGE);
        return list;
    }

    public static List<String> getChargeTypeList(String setType) {
        if (StringUtil.isEmpty(setType)) {
            return Collections.emptyList();
        }

        // 放电工步
        if (isDischargeType(setType)) {
            return getChargeTypeList();
        }

        List<String> list = new ArrayList<>();
        list.add(setType);
        return list;
    }

    /**
     * 工步设定项
     *
     * @param i       序号
     *                1.恒流充电 CI:300mA T:100min UV:100mV
     *                2.恒流恒压放电 CI:300mA T:100min CV:100mV EC:50mA
     *                3.搁置 T:100min
     *                4.恒流放电 CI:300mA T:100min DV:100mV
     *                5.搁置 T:100min
     *                6.恒流恒压放电 CI:300mA T:100min CV:100mV EC:50mA
     *                7.结束
     * @param setting setting
     *                msg += "1.恒流充电 CI:300mA T:100min UV:100mV \r\n";
     *                msg += "2.恒流恒压放电 CI:300mA T:100min CV:100mV EC:50mA \r\n";
     *                msg += "3.搁置 T:100min \r\n";
     *                msg += "4.恒流放电 CI:300mA T:100min DV:100mV \n";
     *                msg += "5.搁置 T:100min \r\n";
     *                msg += "6.恒流恒压放电 CI:300mA T:100min CV:100mV EC:50mA \r\n";
     *                msg += "7.结束 \r\n";
     */
    public static String getChargeTypeName(int i, AddSettings setting, int maxChargeLimitCurrent) {
        String msg = "";
        msg += i + "." + StepNameEnum.STEP_CC.getMsg(setting.getSetType()) + " ";
        switch (setting.getSetType()) {
            // 恒压充电 电流:最大充电限制电流
            case SettingConstant.CONSTANT_VOLTAGE:
                msg += "CI:" + maxChargeLimitCurrent + "mA ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "CV:" + setting.getVoltage() + "mV \r\n";
                break;
            case SettingConstant.CONSTANT_CURRENT:
                msg += "CI:" + setting.getCurrent() + "mA ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "UV:" + setting.getCutOff() + "mV \r\n";
                break;
            case SettingConstant.CONSTANT_POWER_CHARGE:
            case SettingConstant.CONSTANT_CURRENT_CONSTANT_VOLTAGE:
                msg += "CI:" + setting.getCurrent() + "mA ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "CV:" + setting.getVoltage() + "mV ";
                msg += "EC:" + setting.getCutOff() + "mA \r\n";
                break;
            case SettingConstant.SHELVE:
                msg += "T:" + setting.getExpectedTime() + "min \r\n";
                break;
            case SettingConstant.CONSTANT_CURRENT_DISCHARGE:
                msg += "CI:" + setting.getCurrent() + "mA ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "DV:" + setting.getCutOff() + "mV \r\n";
                break;
            case SettingConstant.CONSTANT_RESISTANCE_DISCHARGE:
                msg += "R:" + setting.getCurrent() + "mΩ ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "V:" + setting.getCutOff() + "mV \r\n";
                break;
            case SettingConstant.CONSTANT_POWER_DISCHARGE:
                msg += "P:" + setting.getCurrent() + "mW ";
                msg += "T:" + setting.getExpectedTime() + "min ";
                msg += "V:" + setting.getCutOff() + "mV \r\n";
                break;
            default:
                break;
        }

        return msg;
    }
}
